/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author Oplink OMS
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webems.modules.topo.service;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.modules.cfg.dao.CfgNeDao;
import com.oplink.webems.modules.topo.dao.TopoDao;
import com.oplink.webems.modules.topo.entity.Topo;



/**
 * 告警Service
 * @author Oplink OMS
 * @version 2014-09-01
 */
@Component
@Transactional
public class TopoService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TopoService.class);
	
	@Autowired
	private TopoDao topoDao;
	@Autowired
	private CfgNeDao cfgNeDao;
	
	public Topo get(Long id) {
		return topoDao.findOne(id);
	}
	
	public Topo findByTopoId(Long id) throws Exception {
		List<Topo> list = topoDao.findByTopoId(id);
		if(list.size() == 1){
			return list.get(0);
		}else if(list.size()>1){
			throw new Exception("has more result!");
		}else{
			//没有内容
			return null;
		}
	}
	
	
	public boolean updateTopo(Topo t){
		return topoDao.update(t);
	}
	
	public boolean save(Topo t){
		return topoDao.save(t)!=null;
	}
	
	public boolean addNode(long topoId, long nodeId, String name, int type ) throws Exception{
		Topo topo = this.findByTopoId(topoId);
		String content = topo.getContent();
		JSONObject jo = JSONObject.parseObject(content);
		JSONArray nodes = (JSONArray) jo.get("nodes");
		
		Iterator<Object> it = nodes.iterator();
		while(it.hasNext()){
			JSONObject node = (JSONObject) it.next();
			if(node.getLongValue("id") == nodeId){
				//网元已存在
				return false;
			}
		}
		
		
		
		nodes.add(this.getNode(nodeId, name, type));
		
		topo.setContent(jo.toJSONString());
		
		return this.updateTopo(topo);
	}
	
	public boolean delNode(long topoId, long nodeId) throws Exception{
		Topo topo = this.findByTopoId(topoId);
		String content = topo.getContent();
		JSONObject jo = JSONObject.parseObject(content);
		JSONArray nodes = (JSONArray) jo.get("nodes");
		Iterator<Object> it = nodes.iterator();
		while(it.hasNext()){
			JSONObject node = (JSONObject) it.next();
			if(node.getLongValue("id") == nodeId){
				nodes.remove(node);
				break;
			}
		}
		
		topo.setContent(jo.toJSONString());
		return this.updateTopo(topo);
	}
	
	private JSONObject getNode(long nodeId, String name, int type){
		JSONObject node = new JSONObject();
		node.put("id", nodeId);
		node.put("text", name);
		node.put("type", 0);
		node.put("x", 0);
		node.put("y", 0);
		node.put("alarmLevel", type);
		node.put("image", type==0?"/webems/static/modules/topo/images/server.png":"/webems/static/modules/topo/images/wanjet.png");
		return node;
	}
	
	public Page<Topo> find(Page<Topo> page, Topo topoentity) {
		DetachedCriteria dc = topoDao.createDetachedCriteria();
		//添加查询条件
		dc.add(Restrictions.eq(Topo.DEL_FLAG, Topo.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return topoDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<Topo> getUserTopoentity(Page<Topo> page, Topo topoentity) {
		DetachedCriteria dc = topoDao.createDetachedCriteria();
		//添加查询条件
		dc.add(Restrictions.eq(Topo.DEL_FLAG, Topo.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return topoDao.find(page, dc);
	}
	
	public Topo getTopoentity(Long id) {
		return topoDao.findOne(id);
	}

	/**
	 * 在添加节点时,要添加该节点的网络Topo
	 * 在添加 网元时，要更新该节点的网络Topo
	 * @param entity
	 */
	public void saveTopoentity(Topo entity) {
		topoDao.save(entity);
	}
	
	public void deleteTopoentity(Long id) {
		topoDao.delete(id);
	}
	
	@Autowired
	public void setTopoDao(TopoDao topoentityDao) {
		this.topoDao = topoentityDao;
	}	
}
